#ifndef VIDEOCAPTURE_AVTGIGE_H
#define VIDEOCAPTURE_AVTGIGE_H
#ifndef NO_AVT_GIGE
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>

#if defined WIN32 || defined WIN64
//#define WIN32_LEAN_AND_MEAN
//#include <Windows.h>
//#include <Winsock2.h>
#define _WINDOWS
#endif

#if defined(_LINUX) || defined(_QNX) || defined(_OSX)
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <arpa/inet.h>
#endif

#ifdef _WINDOWS
#include "AvtGigE/inc-pc/PvApi.h"
#else
	#ifdef _LINUX
	#include "AvtGigE/Linux/inc-pc/PvApi.h"
	#else
		#ifdef _OSX
		#include "AvtGigE/OSX/inc-pc/PvApi.h"
		#endif
	#endif
#endif

#include <opencv/cxcore.h>
//#include <cv.h>
//#include <cvaux.h>
//#include <highgui.h>

#ifndef _WINDOWS
#define TRUE 0
#endif

#ifndef MAX_CAMERA_LIST
#define MAX_CAMERA_LIST 20
#endif

#define CH_MONO 1	// Single channel for mono image
#define CH_COLOR 3	// 3 channels for color image

#define GIGE_IMAGE_X 640
#define GIGE_IMAGE_Y 480

typedef struct tCamera {
    unsigned long   UID;
    tPvHandle       Handle;
    tPvFrame        Frame;
}tCamera;


#include "CameraInterfaceBase.h"
//bool gStop = false;

class AvtGigE : public CameraInterface {
public:
	AvtGigE();
	~AvtGigE();
	int probeCam(unsigned long manualIPaddress = (130 << 24) | (123 << 16) | (248 << 8) | (16) );
	bool getCamId(int index, unsigned long &camId, std::string *camName = NULL);
	bool openCam(unsigned long camId);
	bool getCamProperties(char *propId, unsigned long &value);
	bool setCamProperties(char *propId, unsigned long value);
	bool setImageSize(unsigned int width = 640, unsigned int height = 480, unsigned int bytesPerChannel = 1, unsigned int channels = 1);
	IplImage* grabRawImage();
	bool closeCam();
private:
	tCamera			myCamera;
	//int countCameras();
	//bool capture_init(tPvUint32 widthParam = 640, tPvUint32 heightParam = 480);
	//void capture_image();
	//static
	//void capture_exit();
	//void ListCameras();
	void processErrCode(tPvErr errorCode);
	//unsigned int probeCameras(unsigned long manualIPaddress = (130 << 24) | (123 << 16) | (248 << 8) | (16) );
	//bool initCamera(unsigned int camIndex);
	//int OpenCVCaptureProsilicaGigECamera();
	tPvCameraInfoEx	cameraInfo[MAX_CAMERA_LIST];
	tPvIpSettings	ipSettings[MAX_CAMERA_LIST];
	unsigned long	frameSize;
	//tPvErr			Errcode;
	tPvUint32   width, height;
	//IplImage*   rgbImage;// = NULL;
	unsigned int maxProbedCams;
	bool masterMode;
};


#if defined(_LINUX) || defined(_QNX) || defined(_OSX)
void Sleep(unsigned int time);
//{
//    struct timespec t,r;
    
//    t.tv_sec    = time / 1000;
//    t.tv_nsec   = (time % 1000) * 1000000;
    
//    while(nanosleep(&t,&r)==-1)
//        t = r;
//}

void SetConsoleCtrlHandler(void (*func)(int), int junk);
//{
//  signal(SIGINT, func);
//}
#endif

#endif
#endif
